import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/views/layout.vue'
import Login from '@/views/login.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/',
      component: Layout,
      redirect: '/login',
      children: [
        // 公共路由
        {
          path: '/shop',
          name: 'Shop',
          component: () => import('@/views/shop.vue'),
          meta: { title: '文具商城' }
        },
        // 学生路由
        {
          path: '/student',
          name: 'StudentLayout',
          redirect: '/student/tasks/current',
          children: [
            {
              path: 'tasks/current',
              name: 'StudentCurrentTasks',
              component: () => import('@/views/student/tasks/current.vue'),
              meta: { title: '当前任务', role: 'student' }
            },
            {
              path: 'analysis',
              name: 'StudentAnalysis',
              component: () => import('@/views/student/analysis.vue'),
              meta: { title: '学习分析', role: 'student' }
            },
            {
              path: 'interaction',
              name: 'StudentInteraction',
              component: () => import('@/views/student/interaction.vue'),
              meta: { title: '互动交流', role: 'student' }
            }
          ]
        },
        // 家长路由
        {
          path: '/parent',
          name: 'ParentLayout',
          redirect: '/parent/analysis/tasks',
          children: [
            {
              path: 'analysis/tasks',
              name: 'ParentTaskAnalysis',
              component: () => import('@/views/parent/analysis/tasks.vue'),
              meta: { title: '任务详情', role: 'parent' }
            },
            {
              path: 'monitor',
              name: 'ParentMonitor',
              component: () => import('@/views/parent/monitor.vue'),
              meta: { title: '学习监督', role: 'parent' }
            },
            {
              path: 'analysis/progress',
              name: 'ParentProgress',
              component: () => import('@/views/parent/analysis/progress.vue'),
              meta: { title: '完成情况', role: 'parent' }
            }
          ]
        },
        // 教师路由
        {
          path: '/teacher',
          name: 'TeacherLayout',
          redirect: '/teacher/tasks/create',
          children: [
            {
              path: 'tasks/create',
              name: 'TeacherCreateTask',
              component: () => import('@/views/teacher/tasks/create.vue'),
              meta: { title: '创建任务', role: 'teacher' }
            },
            {
              path: 'tasks/manage',
              name: 'TeacherManageTask',
              component: () => import('@/views/teacher/tasks/manage.vue'),
              meta: { title: '任务管理', role: 'teacher' }
            },
            {
              path: 'students',
              name: 'TeacherStudents',
              component: () => import('@/views/teacher/students.vue'),
              meta: { title: '学生管理', role: 'teacher' }
            },
            {
              path: 'data',
              name: 'TeacherData',
              component: () => import('@/views/teacher/data.vue'),
              meta: { title: '数据分析', role: 'teacher' }
            }
          ]
        }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStr = localStorage.getItem('user')
  const user = userStr ? JSON.parse(userStr) : null

  // 如果访问登录页，直接放行
  if (to.path === '/login') {
    next()
    return
  }

  // 未登录，跳转到登录页
  if (!user) {
    next('/login')
    return
  }

  // 检查路由是否需要特定角色
  if (to.meta.role && to.meta.role !== user.role) {
    next(`/${user.role}`) // 跳转到用户对应的首页
    return
  }

  next()
})

export default router
